package com.mongo.service.impl;

import com.mongo.pojo.MongoDb;
import com.mongo.pojo.MybatisPlus;
import com.mongo.service.DbService;
import com.mongodb.client.MongoCollection;
import javafx.util.Pair;
import org.bson.Document;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.ExecutableRemoveOperation;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.UpdateDefinition;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class DbServiceImpl implements DbService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MybatisPlusServiceImpl mybatisPlusService;

    /**
     * 每个操作带上collectionName， 效率提高许多，并且，防止子类错误操作
     * @param mongoDb 对象
     */
    @Override
    public void mongoTemplateSample(MongoDb mongoDb) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(mongoDb.get_id()).and("a2").is(mongoDb.getA2()));

        Update update = new Update();
        update.set("a3", mongoDb.getA3());

        List<MongoDb> list = new ArrayList<>();
        list.add(mongoDb);

        /*
         * 数据库有，就新增， 没有，就修改
         */
        mongoTemplate.upsert(query, update, MongoDb.class, "mongoDb");

        /*
         * 修改方法
         */
        // 无实际意义，不推荐使用
        mongoTemplate.update(MongoDb.class);
        mongoTemplate.updateFirst(query, update, MongoDb.class, "mongoDb");
        mongoTemplate.updateMulti(query, update, MongoDb.class, "mongoDb");
        mongoTemplate.findAndModify(query, update, MongoDb.class, "mongoDb");

        /*
         * 查询方法
         */
        mongoTemplate.find(query, MongoDb.class, "mongoDb");
        mongoTemplate.findOne(query, MongoDb.class, "mongoDb");
        mongoTemplate.findAll(MongoDb.class, "mongoDb");
        mongoTemplate.findById(mongoDb.get_id(), MongoDb.class, "mongoDb");

        /*
         * 删除方法
         */
        mongoTemplate.remove(query, MongoDb.class, "mongoDb");
        mongoTemplate.findAndRemove(query, MongoDb.class, "mongoDb");
        mongoTemplate.findAllAndRemove(query, MongoDb.class, "mongoDb");

        /*
         * 新增方法
         */
        mongoTemplate.save(mongoDb, "mongoDb"); // 同upsert()，没有新增，有修改
        mongoTemplate.insert(mongoDb, "mongoDb");
        mongoTemplate.insertAll(list);


    }

    /**
     * 批量修改 formData
     * @param mongoDbList<MongoDb> mongoDbList
     */
    @Override
    public void updateFormDatas(List<MongoDb> mongoDbList) {

    }

    /**
     * 类似于mongo原生sql的操作
     * 不推荐使用
     */
    public void mongoTempalteAsOriginal() {

        // 查询该集合中的所有文档
        MongoCollection<Document> formDataApprovalDocument = mongoTemplate.getCollection("mongoDb");

        // 新增
//        formDataApprovalDocument.insertOne();
//        formDataApprovalDocument.insertMany();

        // 删除
//        formDataApprovalDocument.deleteOne();
//        formDataApprovalDocument.deleteMany();
//        formDataApprovalDocument.findOneAndDelete();

        // 修改
//        formDataApprovalDocument.updateOne();
//        formDataApprovalDocument.updateMany();
//        formDataApprovalDocument.findOneAndUpdate();
//        formDataApprovalDocument.replaceOne();
//        formDataApprovalDocument.findOneAndReplace();

        // 查询
        formDataApprovalDocument.find();

        // 获取集合name
        mongoTemplate.getCollectionName(MongoDb.class);
    }



    // 插入数据
    @Override
    public void save(MongoDb mongoDb) {
        List<MybatisPlus> plusList = mybatisPlusService.findAll();

        ArrayList<MongoDb> mongoDbArrayList = new ArrayList<>();
        for (MybatisPlus mybatisPlus : plusList) {
            MongoDb mongo = new MongoDb(mybatisPlus.getVerId(), mybatisPlus.getA2(), mybatisPlus.getA3());
            mongoDbArrayList.add(mongo);
        }


        mongoTemplate.insertAll(mongoDbArrayList);

//        for (MybatisPlus mybatisPlus : plusList) {
//            MongoDb mongo = new MongoDb();
//            mongo.set_id(mybatisPlus.getVerId());
//            BeanUtils.copyProperties(mybatisPlus, mongo);
//            mongoTemplate.save(mongo);
//        }
    }

    // 查询所有数据
    @Override
    public List<MongoDb> find() {
        Query query = new Query();
        return mongoTemplate.find(query, MongoDb.class);
    }

    // 根据id list查询数据
    @Override
    public List<MongoDb> findByIdList() {
        List<MybatisPlus> mybatisPlusList = mybatisPlusService.findAll();
        mybatisPlusList = mybatisPlusList.subList(1, 2);

        ArrayList<String> arrayList = new ArrayList<>();
        for (MybatisPlus mybatisPlus : mybatisPlusList) {
            arrayList.add(mybatisPlus.getVerId());
        }
        Query query = new Query(Criteria.where("verId").in(arrayList));
        return mongoTemplate.find(query, MongoDb.class);
    }

    @Override
    public void delete() {
        mongoTemplate.findAllAndRemove(new Query(), MongoDb.class);
    }

    @Override
    public void update() {
        List<MongoDb> mongoDbList = find();
        for (MongoDb mongoDb : mongoDbList) {
            Query query = new Query(Criteria.where("_id").is(mongoDb.get_id()));
            Update update = new Update().set("a2", "9");
            mongoTemplate.updateMulti(query, update, MongoDb.class);
        }
    }


}
